package util.jdbc;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
 * 
 * 服务层工厂类
 * 
 * */
public class ClassPathBeanFactory implements BeanFactory {
	private Map<String, Object> beanMap=new HashMap<String,Object>();
	private static ClassPathBeanFactory factory;
	/**、
	 * 
	 * 初始化
	 * 
	 * */
	private ClassPathBeanFactory() {
		try {
			Properties property=new Properties();
			property.load(ClassPathBeanFactory.class.getClassLoader().getResourceAsStream("BeanConfig.properties"));
			@SuppressWarnings("rawtypes")
			Enumeration keys = property.keys();
			while(keys.hasMoreElements()){
				String key=(String) keys.nextElement();
				String value=property.getProperty(key);
				Object obj=Class.forName(value).newInstance();
				beanMap.put(key,obj);
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获得实例
	 * */
	public static ClassPathBeanFactory getInstance(){
		if(factory==null){
			factory=new ClassPathBeanFactory();
		}
		return factory;
	}
	
	/**
	 * 获得指定服务层实例
	 * */
	@Override
	public Object getBean(String beanClassName) {
		return beanMap.get(beanClassName);
	}

}
